<?php
header("Content-Type:text/html;charset=utf-8");
include_once "util.php";
include_once "diseasePages.php";
include_once "symptonPages.php";
/**
 * @var array $diseasePages
 * @var array $symptonPages
 */
$alias = []; $department = []; $symptons = []; $totalSympton = []; $position = [];
$i = 1; $total = count($diseasePages);
foreach ($diseasePages as $name => $url) {
    $content = getHTML($url . "jbzs/");
    $zztzContent = getHTML($url . "zztz/");
    //抽别名
    preg_match("/<dd><i>别名：<\/i> (.*)<\/dd>/U", $content, $match);
    $alias[$name] = explode(",", str_replace("，", ",", $match[1]));
    //抽相关症状
    preg_match("/相关症状.*<\/dd>/siU", $zztzContent, $match);
    preg_match_all("/http:\/\/jbk.39.net\/zhengzhuang\/\w+\/.*title=\"(.*)\"/siU", $match[0], $matches);
    $symptons[$name] = $matches[1];
    //抽症状库
    foreach ($matches[1] as $sympton) {
        if (array_search($sympton, $totalSympton) == false) $totalSympton[] = $sympton;
    }
    //抽部位
    preg_match("/发病部位.*<\/dd>/siU", $content, $match);
    preg_match_all("/<a href='\/bw\/\w+\/'>(.*)<\/a>/U", $match[0], $matches);
    $position[$name] = $matches[1];
    //抽科室
    preg_match("/就诊科室.*<\/dd>/siU", $content, $departmentContent);
    preg_match_all("/href='\/bw\/\w+\/'>(.*)<\/a>/U", $departmentContent[0], $matches);
    $department[$name] = $matches[1];
    echo "Disease Page Analyzed: {$i}/{$total}\n"; $i++;
}
$i = 1; $total = count($symptonPages);
foreach ($symptonPages as $name => $url) {
    $content = getHTML($url . "zzqy/");
    if (!in_array($name, $totalSympton)) $totalSympton[] = $name;
    //抽症状相关疾病
    preg_match("/可能疾病.*<\/table>/siU", $content, $possibleDiseases);
    preg_match_all("/http:\/\/jbk.39.net\/\w+\/.*title=\"(.*)\"/U", $possibleDiseases[0], $matches);
    foreach ($matches[1] as $disease) {
        if (!isset($symptons[$disease])) $symptons[$disease] = [];
        if (!in_array($name, $symptons[$disease])) $symptons[$disease][] = $name;
    }
    echo "Sympton Page Analyzed: {$i}/{$total}\n"; $i++;
}
dumpVariableToFile($alias, "result/alias.php", "alias");
dumpVariableToFile($department, "result/department.php", "department");
dumpVariableToFile($symptons, "result/symptons.php", "symptons");
dumpVariableToFile($totalSympton, "result/totalSympton.php", "totalSympton");